<?php 

namespace app\admin\controller;

use think\Db;
use app\common\validate\UserValidate;

class User extends Base
{
	protected $datas;

	/**
	 * 用户列表
	 * @Author   fanzhongxing
	 * @DateTime 2020-04-30T22:51:53+0800
	 * @return   [type]                   [description]
	 */
	public function userList()
	{
		$data = Db::name('t_user')->paginate(10);
		$this->assign('data', $data);
		return $this->fetch();
	}

	/**
	 * 添加用户
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T11:13:18+0800
	 */
	public function addUser()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$addValidate = new UserValidate();
			if (!$addValidate->scene('add')->check($this->datas)) {
	            $this->error($addValidate->getError());
	        }
	        $username = $this->datas['username'];
	        $mobile = $this->datas['mobile'];
	        $password = md5($this->datas['password']);
	        $repassword = md5($this->datas['repassword']);
	        $avatar = isset($this->datas['avatar']) ? $this->datas['avatar'] : '';
	        if ($password != $repassword) {
	        	$this->error('密码和确认密码不一致');
	        }
	        $insertData = [
	        	'username' => $username,
	        	'password' => $password,
	        	'mobile' => $mobile,
	        	'avatar' => $avatar,
	        	'create_time' => time()
	        ];
	        $result = Db::name('t_user')->insert($insertData);
	        if ($result) {
	        	$this->success('添加成功');
	        }
	        $this->error('添加失败');
		}
		return $this->fetch();
	}

	/**
	 * 上传图片
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T14:09:27+0800
	 * @return   [type]                   [description]
	 */
	public function uploadImg()
	{
		$path = 'static'. DS . 'upload' . DS . 'user_avatar';
		$size = 58400*100;
		$type = 'jpg,png,gif,jpeg';
		$uploadResult = uploadFile('file', $path, $size, $type);
		if (!$uploadResult[0]) {
			$this->error($uploadResult[1]);
		}
		$this->success('上传成功', '', $uploadResult[1]);
	}

	/**
	 * 修改用户
	 * @Author   fanzhongxing
	 * @DateTime 2020-04-30T22:57:29+0800
	 * @return   [type]                   [description]
	 */
	public function editUser()
	{
		$this->datas = $this->params;
		if (request()->isPost()) {
			$editValiate = new UserValidate();
			if (!$editValiate->scene('edit')->check($this->datas)) {
				$this->error($editValiate->getError());
			}
			$uid = $this->datas['uid'];
			$password = isset($this->datas['password']) ? md5($this->datas['password']) : '';
			$repassword = isset($this->datas['repassword']) ? md5($this->datas['repassword']) : '';
			if ($password != $repassword) {
				$this->error('密码和确认密码不一致');
			}
			if (!empty($password)) {
				$updateData['password'] = $password;
			}
			$updateData['username'] = $this->datas['username'];
			$updateData['mobile'] = $this->datas['mobile'];
			$updateData['avatar'] = $this->datas['avatar'];
			$updateData['update_time'] = time();
			$result = Db::name('t_user')->where('id', $uid)->update($updateData);
			if (!$result) {
				$this->error('修改失败');
			}
			$this->success('修改成功');
		}
		$uid = $this->datas['uid'];
		$userData = Db::name('t_user')->where('id', $uid)->find();
		$this->assign('list', $userData);
		return $this->fetch();
	}

	/**
	 * 删除用户
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T14:51:28+0800
	 * @return   [type]                   [description]
	 */
	public function delUser()
	{
		if (request()->isGet()) {
			$this->datas = $this->params;
			$delValidate = new UserValidate();
			if (!$delValidate->scene('del')->check($this->datas)) {
				$this->error($delValidate->getError());
			}
			$uid = $this->datas['uid'];
			$result = Db::name('t_user')->where('id', $uid)->delete();
			if (!$result) {
				$this->error('删除失败');
			}
			$this->success('删除成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 批量删除
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T18:04:15+0800
	 * @return   [type]                   [description]
	 */
	public function delAllUser()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$delAllValidate = new UserValidate();
			if (!$delAllValidate->scene('delAll')->check($this->datas)) {
				$this->error($delAllValidate->getError());
			}
			$ids = $this->datas['ids'];
			$result = Db::name('t_user')->whereIn('id', $ids)->delete();
			if (!$result) {
				$this->error('删除失败');
			}
			$this->success('删除成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 禁用或启用
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T16:16:12+0800
	 */
	public function ProhibitOrEnable()
	{
		if (request()->isGet()) {
			$this->datas = $this->params;
			$ProOrEnaValidate = new UserValidate();
			if (!$ProOrEnaValidate->scene('ProOrEna')->check($this->datas)) {
				$this->error($ProOrEnaValidate->getError());
			}
			$uid = $this->datas['uid'];
			$msg = $this->datas['msg'];
			if ($msg == '禁用') {
				$status = 0;
			} else {
				$status = 1;
			}
			$result = Db::name('t_user')->where('id', $uid)->update(['status' => $status]);
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 搜索
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T20:10:56+0800
	 * @return   [type]                   [description]
	 */
	public function sreach()
	{
		$this->datas = $this->params;
		$username = isset($this->datas['username']) ? $this->datas['username'] : '';
		$mobile = isset($this->datas['mobile']) ? $this->datas['mobile'] : '';
		if (empty($username) && empty($mobile)) {
			$this->error('请输入要搜索的内容');
		}
		if (!empty($username) && empty($mobile)) {
			$data = Db::name('t_user')->whereLike('username', '%' . $username . '%')->paginate(6);
		}
		if (!empty($mobile) && empty($username)) {
			$data = Db::name('t_user')->where('mobile', $mobile)->paginate(6);
		}
		if (!empty($username) && !empty($mobile)) {
			$data = Db::name('t_user')->where('mobile', $mobile)->whereLike('username', '%' . $username . '%')->paginate(6);
		}
		$this->assign('data', $data);
		return $this->fetch('user_list');
	}


}